package com.example.springboot.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.Random;
import java.util.UUID;


public class UUIDUtils {
	/**
	 * 产生一个32位的GUID
	 * 
	 * @return
	 */
	public static String newGUID() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString().replace("-", "");
	}

	/**
	 * 获取32位GUID
	 * 
	 * @return
	 */
	public static String getId() {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			UUID uuid = UUID.randomUUID();
			String guidStr = uuid.toString();
			md.update(guidStr.getBytes(), 0, guidStr.length());
			return new BigInteger(1, md.digest()).toString(16);
		} catch (NoSuchAlgorithmException e) {
			return null;
		}
	}
	
	public static void main(String[] args) throws ParseException {  
        // TODO Auto-generated method stub  
        System.out.println(getId());  

    }  
	
	/**
	 * yanghui 
	 * 2016-10-20
	 * 生成主键(19位数字) 
	 * 主键生成方式,年月日时分秒毫秒的时间戳+四位随机数保证不重复 
	 */    
    public static Long getNumId() {  
	//当前系统时间戳精确到毫秒  
	Long simple=System.currentTimeMillis();  
	//三位随机数  
	int random=new Random().nextInt(900000)+100000;//为变量赋随机值100000-99999;  
	return Long.decode((simple.toString()+random));    
    }    
}
